package com.je.app.persistence;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;

import com.je.app.model.Sesion;
import com.je.app.model.Turno;

public class DaoTurno extends DaoGeneric{

	private static DaoTurno instance = new DaoTurno();
	
	public static DaoTurno getInstance() {

		return instance;
	}
	
	private DaoTurno(){
		
	}
	
	@SuppressWarnings("deprecation")
	public List<Turno> filter(Date fecha, Integer idHabitacion){
		
		String query = "FROM Turno t WHERE terminado=1 ";

		if(fecha != null){
			java.sql.Date inferior = new java.sql.Date(fecha.getTime());
			fecha.setDate(fecha.getDate()+1);
			java.sql.Date superior = new java.sql.Date(fecha.getTime());
			query = query + " and ( ( hora_inicio >= '" + inferior + "' and hora_inicio <='" + superior + "' ) or " ;
			query = query + " ( hora_fin >= '" + inferior + "' and hora_fin <='" + superior + "' ) ) " ;
		}

		if(idHabitacion != null){
			query = query + " and id_habitacion = " + idHabitacion;
		}

		query = query + " ORDER BY t.idTurno DESC ";
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
        List<Turno> filteredList = session.createQuery(query).list();
        session.getTransaction().commit();
        
        return filteredList;

	}
	
	public List<Turno> list(String className){
		
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
        List<Turno> list = session.createQuery("FROM "+className+" WHERE terminado=1").list();
        session.getTransaction().commit();
        
        return list;
	}

	@SuppressWarnings("unchecked")
	public List <Turno> getInitiated(){
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
        List <Turno> turnosIniciados = session.createQuery(" FROM Turno WHERE terminado = 0 ").list();
        session.getTransaction().commit();
        return turnosIniciados;
	}

	public List<Turno> list(Sesion sesionActual) {
    	Session session = HibernateUtil.getSession();
        session.beginTransaction();
        List<Turno> list = session.createQuery(" FROM Turno t WHERE t.sesion.idSesion = " + sesionActual.getIdSesion() + " and terminado=1").list();
        session.getTransaction().commit();
        
        return list;
	}


}
